package sk.fiit.vppj.eventmanager.gui.administrator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import sk.fiit.vppj.eventmanager.core.DatabaseManager;
import sk.fiit.vppj.eventmanager.core.UserManager;
import sk.fiit.vppj.eventmanager.data.BasicProject;


/**
 * Table model responsible for manipulation with table in administrator project's tab
 */
final class ProjectTableModel extends DefaultTableModel {
	private String[] columnNames = { "ID", "Name", "Usertype", "Class" };
	private List<BasicProject> data = null;
	private static final int PAGINATION = 25;

	public ProjectTableModel() {
	}

	@Override
	public int getRowCount() {
		if( this.data == null ) {
			return 0;
		}
		return this.data.size();
	}

	@Override
	public int getColumnCount() {
		return this.columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return this.columnNames[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt( 0, columnIndex ).getClass();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if( columnIndex == 0 ) {
			return data.get( rowIndex ).getId();
		}
		else if( columnIndex == 1 ) {
			return data.get( rowIndex ).getName();
		}
		else if( columnIndex == 2 ) {
			return data.get( rowIndex ).getCategory();
		}
		else if( columnIndex == 3 ) {
			return data.get( rowIndex ).getStatus();
		}
		else {
			return data.get( rowIndex ).getId();
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub

	}

	public int getPagination() {
		return PAGINATION;
	}

	private PreparedStatementCreator prepareStatementJudge(final int page) {
		final String sql = "SELECT p.IDProject, p.nameEng, "
				+ " jos_event_manager_competitioncategories.name AS catName , p.status FROM jos_event_manager_projects AS p "
				+ " JOIN jos_event_manager_judgecategories AS j ON j.category_id = p.category_id "
				+ " JOIN jos_event_manager_competitioncategories ON j.category_id = jos_event_manager_competitioncategories.IDComCategory "
				+ " WHERE p.event_id = ? AND j.user_id = ? "
				+ " AND p.status !='accepted' AND p.status !='refused' ORDER BY p.nameEng LIMIT ?, ? ";

		return new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement( sql );
				ps.setInt( 1, UserManager.getInstance().getUser().getEvent_id() );
				ps.setInt( 2, UserManager.getInstance().getUser().getId() );
				ps.setInt( 3, page * PAGINATION );
				ps.setInt( 4, PAGINATION );

				return ps;
			}
		};
	}

	private PreparedStatementCreator prepareStatementAdmin(final int page) {
		final String sql = "SELECT p.IDProject, p.nameEng, "
				+ " c.name AS catName , p.status FROM jos_event_manager_projects AS p "
				+ " JOIN jos_event_manager_competitioncategories AS c ON p.category_id = c.IDComCategory "
				+ " WHERE p.event_id = ? ORDER BY p.nameEng LIMIT ?, ? ";

		return new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement( sql );
				ps.setInt( 1, UserManager.getInstance().getUser().getEvent_id() );
				ps.setInt( 2, page * PAGINATION );
				ps.setInt( 3, PAGINATION );

				return ps;
			}
		};
	}

	public void prepareData(final int page) {

		PreparedStatementCreator s = null;
		if( UserManager.getInstance().getUser().getClazz().equals( "judge" ) ) {
			s = prepareStatementJudge( page );
		}
		else {
			s = prepareStatementAdmin( page );
		}
		this.data = DatabaseManager.getInstance().getJdbcTemplate().query( s, new RowMapper<BasicProject>() {

			public BasicProject mapRow(ResultSet rs, int rowNum) throws SQLException {
				BasicProject tempP = new BasicProject();
				tempP.setId( rs.getInt( "IDProject" ) );
				tempP.setName( rs.getString( "nameEng" ) );
				tempP.setCategory( rs.getString( "catName" ) );
				tempP.setStatus( rs.getString( "status" ) );

				return tempP;
			}

		} );
	}

}
